﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumTracker.Models;

namespace ScrumTracker.Controllers
{
    public class SprintsController : Controller
    {
        //
        // GET: /Sprints/

        public ActionResult Get(int id)
        {
            //var context = new ScrumTrackerEntities();
            //ViewData.Model = context.Sprints.Include("Stories").SingleOrDefault(s => s.SprintId == id);
            //return View("Stories");
            return RedirectToAction("stories");
        }

        public ActionResult Stories(int id)
        {
            var context = new ScrumTrackerEntities();
            ViewData.Model = context.Sprints.Include("Stories").SingleOrDefault(s => s.SprintId == id);
            return View();
        }

        public ActionResult Items(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var sprint = context.Sprints.Include("Stories").SingleOrDefault(p => p.SprintId == id);
            var stories = sprint.Stories.OrderByDescending(s => s.Priority).ThenBy(s => s.Effort).ToList();
            return PartialView("~/Views/Stories/_SprintList.cshtml", stories);
        }

        public ActionResult New(Sprint model)
        {
            ModelState.Clear();
            model.EndDate = DateTime.UtcNow.AddDays(7);
            ViewData.Model = model;
            return View();
        }

        [HttpPost]
        public ActionResult Create()
        {
            var entity = new Sprint() { CreatedAt = DateTime.UtcNow };
            if (TryUpdateModel<Sprint>(entity))
            {
                var context = ScrumTrackerEntities.Instance;
                context.Sprints.AddObject(entity);
                context.SaveChanges();
                TempData["notice"] = "Sprint successfully added!";
                return RedirectToAction("Get", "Sprints", new { id = entity.SprintId });
            }
            //return Redirect(Request.UrlReferrer.OriginalString + "#sprints");
            return View("New");
        }

        public ActionResult Edit(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            ViewData.Model = context.Sprints.SingleOrDefault(e => e.SprintId == id);
            return View();// PartialView("_Edit");
        }

        [HttpPost]
        public ActionResult Update(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var entity = context.Sprints.SingleOrDefault(e => e.SprintId == id);
            if (TryUpdateModel<Sprint>(entity))
            {
                context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                context.SaveChanges();
                TempData["notice"] = "Sprint successfully edited!";
            }
            ViewData.Model = entity;
            return View("Edit");
        }

        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var entity = context.Sprints.SingleOrDefault(e => e.SprintId == id);
            entity.IsDeleted = true;
            context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
            context.SaveChanges();
            TempData["notice"] = "Sprint successfully deleted!";
            return RedirectToAction("Get", "Products", new { id = entity.ProductId });
        }
    }
}
